package entities.controllers;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import entities.kinds.Skill;
import entities.pmf.PMF;

public class Skills {
	static final PersistenceManager pm = PMF.getInstance().getPersistenceManager();

	public static void add(String type, String login) {
		try {
			Skill skill = new Skill();
			skill.setType(type);
			skill.setUser(login);
			pm.makePersistent(skill);
		} catch (Exception e) {
			System.out.println("skill add err:" + e);
		}
	}

	public static List<Skill> getByType(String type) {
		Query q = pm.newQuery(Skill.class);
		q.setFilter("type == type_");
		q.declareParameters("String type_");
		q.setRange(0, 1000);
		@SuppressWarnings("unchecked")
		List<Skill> res = (List<Skill>) q.execute(type);
		return res;
	}

	public static String[] getByUser(String user) {
		Query q = pm.newQuery(Skill.class);
		q.setFilter("user == user_");
		q.declareParameters("String user_");
		q.setRange(0, 1000);
		@SuppressWarnings("unchecked")
		List<Skill> res = (List<Skill>) q.execute(user);
		String[] s = new String[res.size()];
		for (int i = 0; i < res.size(); i++) {
			s[i] = res.get(i).getType();
		}
		return s;
	}

}
